﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Infrastructure.Caching;
using Ue.Infrastructure.Caching.VolatileProvider;

namespace Ue.Tests.Infrastructure.Caching.VolatileProvider
{
    /// <summary>
    /// Signals 的摘要说明
    /// </summary>
    [TestClass]
    public class SignalsTests
    {
        ICacheManager cacheManager;
        ISignals signal;

        [TestInitialize]
        public void Initial()
        {
            this.cacheManager = new CacheManager(typeof(CacheTests), new CacheHolder(new CacheContextAccessor()));
            this.signal = new Signals();
        }

        [TestMethod]
        public void TriggerReturnNewCacheItem()
        {
            object obj=new object();
            int cached = 0;

            Func<int> retrieve = () => this.cacheManager.Get("testItem", ctx =>
            {
                ctx.Monitor(this.signal.When(obj));

                return ++cached;
            });

            ///第一次执行，将对象加入缓存
            Assert.AreEqual(1, retrieve());

            //第二次执行，从缓存中取出未过期对象
            Assert.AreEqual(1, retrieve());

            //触发过期机制，缓存失效
            this.signal.Trigger(obj);
            Assert.AreEqual(2, retrieve());
        }
    }
}
